package edu.osumc.bmi.soap.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import edu.osumc.bmi.soap.schema.AddUserRequest;
import edu.osumc.bmi.soap.schema.DeleteUserRequest;
import edu.osumc.bmi.soap.schema.GetAllUsersRequest;
import edu.osumc.bmi.soap.schema.GetAllUsersResponse;
import edu.osumc.bmi.soap.schema.GetUserRequest;
import edu.osumc.bmi.soap.schema.GetUserResponse;
import edu.osumc.bmi.soap.schema.UpdateUserRequest;
import edu.osumc.bmi.soap.schema.User;
import edu.osumc.bmi.soap.service.SoapService;
import edu.osumc.bmi.soap.util.SoapException;

@Endpoint
public class UserEndpointImpl implements UserEndpoint {
	private SoapService soapService;

	@Autowired
	public UserEndpointImpl(@Qualifier("soapService") SoapService soapService) {
		this.soapService = soapService;
	}

	@PayloadRoot(localPart = UserEndpoint.ADD_USER_REQUEST, namespace = SoapService.NAMESPACE)
	public void addUser(AddUserRequest request) throws SoapException {
		soapService.addUser(request.getUser());
	}

	@PayloadRoot(localPart = UserEndpoint.DELETE_USER_REQUEST, namespace = SoapService.NAMESPACE)
	public void deleteUser(DeleteUserRequest request) throws SoapException {
		soapService.deleteUser(request.getId());
	}

	@PayloadRoot(localPart = UserEndpoint.GET_ALL_USERS_REQUEST, namespace = SoapService.NAMESPACE)
	public GetAllUsersResponse getAllUsers(GetAllUsersRequest request) {
		GetAllUsersResponse getAllusersResponse = new GetAllUsersResponse();
		for (User user : soapService.getAllUsers()) {
			getAllusersResponse.getUser().add(user);
		}
		return getAllusersResponse;
	}

	@PayloadRoot(localPart = UserEndpoint.GET_USER_REQUEST, namespace = SoapService.NAMESPACE)
	public GetUserResponse getUser(GetUserRequest request) throws SoapException {
		GetUserRequest getUserRequest = (GetUserRequest) request;
		User user = soapService.getUser(getUserRequest.getId());
		GetUserResponse getUserResponse = new GetUserResponse();
		getUserResponse.setUser(user);
		return getUserResponse;
	}

	@PayloadRoot(localPart = UserEndpoint.UPDATE_USER_REQUEST, namespace = SoapService.NAMESPACE)
	public void updateUser(UpdateUserRequest request) throws SoapException {
		soapService.updateUser(request.getUser());
	}

}
